<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>

    function curry (fn) {

      function curried (...args) {
      
        function next(...nextArgs) {
          if (nextArgs.length === 0) {
            return fn(...args)
          } 
          
          return curried(...args, ...nextArgs)
        }

        next.valueOf = () => {
          return fn(...args)
        }

        return next
      }

      return curried
    }

    function add (...args) {
      console.log('add', ...args)
      return args.reduce((pre, next) => pre + next, 0)
    }

    const currySum = curry(add)
    console.log(+currySum(1)(3)(5))

  </script>
</body>
</html>